/**
 * Flight Infomation Resource
 */
package com.dlplay.flightinfo.server.resource;

import java.io.IOException;
import java.util.logging.Level;

import org.restlet.data.MediaType;
import org.restlet.ext.xml.DomRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;

import com.dlplay.flightinfo.core.FlightSystem;
import com.dlplay.flightinfo.data.AirFlightCode;
import com.dlplay.flightinfo.data.FlightInfo;
import com.dlplay.flightinfo.data.RealTimeFlightInfo;
import com.dlplay.flightinfo.presentation.XMLFlightGenerator;
import com.dlplay.flightinfo.presentation.XMLRealTimeFlightGenerator;

/**
 * @author zhao yu
 *
 */
public class FlightResource extends ServerResource {

	private String flightNum;
	private RealTimeFlightInfo flight;

	/*
	 * (non-Javadoc)
	 * @see org.restlet.resource.UniformResource#doInit()
	 */
	@Override
	protected void doInit() throws ResourceException {
		// Get the "itemName" attribute value taken from the URI template
        // /FlightInfo/{flightNum}
        this.flightNum = (String) getRequest().getAttributes().get("flightNum");
        AirFlightCode code = flightNumberMapping(flightNum);
        // Get the item directly from the "persistence layer".
        this.flight = FlightSystem.getFlightManager().getRealTimeFlightInfo(code);

        setExisting(this.flight != null);
	}
	
	private AirFlightCode flightNumberMapping(String flightNum2) {
		// TODO id_7, mapping
		return null;
	}

	/*
	 * 
	 */
	 @Get("xml")
	    public Representation toXml() {
		try {
			getLogger().info("Create Flight XML");
			DomRepresentation representation = new DomRepresentation(
			         MediaType.TEXT_XML);
			XMLRealTimeFlightGenerator generator = new XMLRealTimeFlightGenerator(flight);
			generator.generate(representation.getDocument());
			return representation;
		} catch (IOException e) {
			getLogger().log(Level.SEVERE, "Create DomRepresentation Failed");
			e.printStackTrace();
		}
		return null;
		 
	 }
	 
	 /*
	  * 
	  */
	 
	 	
}
